package cn.whb.hrm.service.impl;

import cn.hutool.http.HttpUtil;
import cn.whb.hrm.domain.LoginUser;
import cn.whb.hrm.domain.UserMeal;
import cn.whb.hrm.dto.LoginDto;
import cn.whb.hrm.dto.RefreshDto;
import cn.whb.hrm.mapper.LoginUserMapper;
import cn.whb.hrm.mapper.UserMealMapper;
import cn.whb.hrm.prop.AuthDetailProperties;
import cn.whb.hrm.prop.AuthDetails;
import cn.whb.hrm.service.ILoginUserService;
import cn.whb.hrm.util.AjaxResult;
import cn.whb.hrm.util.encrypt.ValidUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whb
 * @since 2021-12-16
 */
@Service
public class LoginUserServiceImpl extends ServiceImpl<LoginUserMapper, LoginUser> implements ILoginUserService {

    @Autowired
    private LoginUserMapper loginUserMapper;
    @Autowired
    private UserMealMapper userMealMapper;

    @Override
    public AjaxResult entering(LoginUser loginUser) {
        //保存t_login_user
        loginUserMapper.insert(loginUser);
        //保存t_user_meal
        UserMeal userMeal = new UserMeal();
        //设置返回的自增主键
        userMeal.setLoginId(loginUser.getId());
        userMeal.setMealId(loginUser.getMealId());
        userMeal.setExpireDate(new Date());
        userMealMapper.insert(userMeal);
        return AjaxResult.me().setSuccess(true).setMessage("新增成功").setResultObj(loginUser.getId());
    }

    @Override
    public AjaxResult register(LoginUser loginUser) {
        loginUserMapper.insert(loginUser);
        return AjaxResult.me().setResultObj(loginUser.getId());
    }

    @Autowired
    private AuthDetailProperties properties;

    @Override
    public AjaxResult login(LoginDto dto) {
        //非空校验使用jsr303
        String url = "http://localhost:3010/oauth/token?client_id=%s&client_secret=%s&grant_type=password&redirect_uri=http://www.baidu.com&username=%s&password=%s";
        //获取type
        Integer type = dto.getType();
        /*String clientId = "";
        String clientSecret = "";
        //判断type类型
        if (type==0){ //是后台登录
            clientId = "admin";
            clientSecret = "1";
        }else { //前台登录
            clientId = "website";
            clientSecret = "1";
        }*/
        AuthDetails authDetails = properties.getAuthDetails(type);
        ValidUtils.assertNotNull(authDetails, "没有该类型用户");
        //替换url中的字符
        url = String.format(url, authDetails.getClientId(), authDetails.getClientSecret(), dto.getUsername(), dto.getPassword());
        //发送请求，得到一个json
        String data = HttpUtil.get(url);
        //将json格式转为map
        Map<String,Object> map = JSON.parseObject(data, Map.class);
        return AjaxResult.me().setResultObj(map);
    }

    @Override
    public AjaxResult refresh(@Valid RefreshDto dto) {
        String url="http://localhost:3010/oauth/token?grant_type=refresh_token&refresh_token=%s&client_id=%s&client_secret=%s";
        AuthDetails authDetails = properties.getAuthDetails(dto.getType());
        ValidUtils.assertNotNull(authDetails, "抱歉，没有该类型用户");
        String clientId = authDetails.getClientId();
        String clientSecret = authDetails.getClientSecret();
        String refreshUrl = String.format(url, dto.getRtoken(), clientId, clientSecret);
        String newToken = HttpUtil.get(refreshUrl);
        Map<String,Object> map = JSON.parseObject(newToken, Map.class);
        return AjaxResult.me().setResultObj(map);
    }
}
